﻿using BepInEx;
using BepInEx.Logging;
using System;
using UnityEngine;
using UnityEngine.UI;
using HarmonyLib;
using System.Collections.Generic;
using static System.Net.Mime.MediaTypeNames;

namespace RatopiaCreateModeMod;

[BepInPlugin("RatopiaCreateModeMod", "RatopiaCreateModeMod", "1.0.0")]
public class CreativeMod : BaseUnityPlugin
{
    internal static new ManualLogSource Logger;

    private const string OPEN = "开";
    private const string CLOSE = "关";

    private static Vector2 ModUISize = new Vector2(800f, 600f);
    private static Vector2 ScreenStandardSize = new Vector2(1920f, 1080f);
    private Rect windowRect = new Rect((float)Screen.width / 2f, (float)Screen.height / 2f, ModUISize.x, ModUISize.y);

    private static bool ActiveModUI;
    private static bool ActiveQueenTeleport;
    private static bool ActiveInstantDemolition;
    private static bool ActiveCheatUI;

    private Coroutine QueenTeleportCoroutine;


    private Vector2 ScreenSize
    {
        get
        {
            return new Vector2((float)Screen.width, (float)Screen.height);
        }
    }
    private Vector2 ScreenSizeRatio
    {
        get
        {
            return this.ScreenSize / ScreenStandardSize;
        }
    }

    private void OnGUI()
    {
        this.windowRect = new Rect(this.windowRect.x, this.windowRect.y, ModUISize.x * this.ScreenSizeRatio.x, ModUISize.y * this.ScreenSizeRatio.y);
        if (ActiveModUI)
        {
            this.windowRect = GUILayout.Window(0, this.windowRect, new GUI.WindowFunction(this.StartGameWindowFunc), "暑托邦创造模式mod", Array.Empty<GUILayoutOption>());
        }
    }

    private void StartGameWindowFunc(int id)
    {
        var str_QueenInvincibility = DebugMgr.Instance._CheatMgr.m_QueenInvincibility ? OPEN : CLOSE;
        if (GUILayout.Button($"鼠鼠女王无敌:{str_QueenInvincibility}"))
        {
            DebugMgr.Instance._CheatMgr.m_QueenInvincibility = !DebugMgr.Instance._CheatMgr.m_QueenInvincibility;
        }

        var str_QueenTeleport = ActiveQueenTeleport ? OPEN : CLOSE;
        if (GUILayout.Button($"鼠鼠女王传送:{str_QueenTeleport}"))
        {
            ActiveQueenTeleport = !ActiveQueenTeleport;
            if (ActiveQueenTeleport)
            {
                QueenTeleportCoroutine = StartCoroutine(QueenTeleportC());
            }
            else
            {
                StopCoroutine(QueenTeleportCoroutine);
                QueenTeleportCoroutine = null;
            }
        }

        var str_NoNeedMat = DebugMgr.Instance._CheatMgr.m_NoNeedMat ? OPEN : CLOSE;
        if (GUILayout.Button($"建造无需材料:{str_NoNeedMat}"))
        {
            DebugMgr.Instance._CheatMgr.m_NoNeedMat = !DebugMgr.Instance._CheatMgr.m_NoNeedMat;
        }

        var str_BP_SkipCheat = DebugMgr.Instance._CheatMgr.m_BP_SkipCheat ? OPEN : CLOSE;
        if (GUILayout.Button($"跳过蓝图:{str_BP_SkipCheat}"))
        {
            DebugMgr.Instance._CheatMgr.m_BP_SkipCheat = !DebugMgr.Instance._CheatMgr.m_BP_SkipCheat;
        }

        var str_ActiveInstantDemolition = ActiveInstantDemolition ? OPEN : CLOSE;
        if (GUILayout.Button($"瞬间拆除:{str_ActiveInstantDemolition}"))
        {
            ActiveInstantDemolition = !ActiveInstantDemolition;
        }

        if (GUILayout.Button("解锁全部科技（不可逆，使用前记得保存）"))
        {
            DebugMgr.Instance._CheatMgr.Unlock_All_ResearchBtn();
            DebugMgr.Instance._CheatMgr.UnlockEncyclopedia();
            DebugMgr.Instance._CheatMgr.Q_Unlock_Btn();
            DebugMgr.Instance._CheatMgr.UnlockAll_QueenItem();
        }

        if (GUILayout.Button("清除迷雾（不可逆，使用前记得存档）"))
        {
            DebugMgr.Instance._CheatMgr.FOG_Off_Btn();
        }

        if (GUILayout.Button("清除掉落物"))
        {
            DebugMgr.Instance._CheatMgr.TObj_Clear_Btn();
        }

        var str_ActiveCheatUI = ActiveCheatUI ? OPEN : CLOSE;
        if (GUILayout.Button($"开发者界面:{str_ActiveCheatUI}"))
        {
            ActiveCheatUI = !ActiveCheatUI;
            DebugMgr.Instance._CheatMgr.SetActive(ActiveCheatUI);
        }
    }

    private IEnumerator<float> QueenTeleportC()
    {
        while (ActiveQueenTeleport)
        {
            if (Input.GetMouseButtonUp(1))
            {
                C_Node node = GameMgr.Instance._TileMgr.GetNode(GameMgr.Instance._CamMgr.GetMousePos());
                if (node != null && C_Node.CanIOnBox(node))
                {
                    GameMgr.Instance._T_UnitMgr.m_Queen.Tf.position = new Vector3((float)node.x, (float)node.y, GameMgr.Instance._T_UnitMgr.m_Queen.Tf.position.z);
                    GameMgr.Instance._T_UnitMgr.m_Queen.NodeUpdate();
                }
            }
            yield return 0f;
        }
        yield break;
    }

    private void Awake()
    {
        Harmony.CreateAndPatchAll(typeof(CreativeMod), null);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F9))
        {
            ActiveModUI = !ActiveModUI;
        }
    }

    [HarmonyPostfix]
    [HarmonyPatch(typeof(WorkMark), "MarkRefresh", new Type[] { typeof(Building) })]
    public static void WorkMark_MarkRefresh_Building_Postfix(WorkMark __instance)
    {
        if (ActiveInstantDemolition && __instance.m_Kind == WorkMarkKind.Demolition && __instance.m_Building != null)
        {
            __instance.m_Building.BuildingDemolition(false);
        }
    }


    [HarmonyPostfix]
    [HarmonyPatch(typeof(WorkMark), "MarkSet")]
    public static void WorkMark_MarkSet_Postfix(WorkMark __instance)
    {
        if (ActiveInstantDemolition && __instance.m_Kind == WorkMarkKind.Mining && __instance.m_Tile != null)
        {
            __instance.m_Tile.DestroyTile(false);
        }
    }

    [HarmonyPrefix]
    [HarmonyPatch(typeof(LoadingSceneMgr), "Start")]
    public static void LoadingSceneMgr_Start()
    {
        Debug.Log("LoadingSceneMgr_Start");
    }

}
